//
//  KKMyQuestionVC.m
//  DoMi
//
//  Created by 康凯 on 2016/11/16.
//  Copyright © 2016年 kk. All rights reserved.
//

#import "KKMyQuestionVC.h"
#import "KKTopSelectView.h"
#import "KKMyHttp.h"
#import "KKCollectDocumentCell.h"
#import "KKMyXuanShangM.h"
#import "KKXuanShangDetailVC.h"

@interface KKMyQuestionVC ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UIView *topView;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIButton *allBtn;

//0代表悬赏1代表提问
@property (nonatomic,assign) int flag;
@property(nonatomic,strong)UIButton *currentBtn;
@property (nonatomic,copy) NSString *type;
@property(nonatomic,strong)NSArray *datas;
@end

@implementation KKMyQuestionVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title=@"我的悬赏/提问";
    [self initView];
}

#pragma mark 初始化view
-(void)initView{
    self.flag=0;
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    
    KKTopSelectView *topItem=[[[NSBundle mainBundle] loadNibNamed:@"KKTopSelectView" owner:nil options:nil] lastObject];
    [topItem initData:@[@"悬赏",@"提问"]];
    topItem.selectBlock=^(int flag){
        self.flag=flag;
        if(flag==0){
            [self getXuanShang];
        }else{
            [self getQuestion];
        }
    };
    [self.topView addSubview:topItem];
    
    self.type=@"all";
    self.currentBtn=self.allBtn;
    self.allBtn.selected=YES;
    //取消group样式头部空白
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, screenWidth, 0.1f)];
    [self.tableView registerNib:[UINib nibWithNibName:@"KKCollectDocumentCell" bundle:nil] forCellReuseIdentifier:@"KKCollectDocumentCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"KKBuyVideoCell" bundle:nil] forCellReuseIdentifier:@"KKBuyVideoCell"];
    
    [self getXuanShang];
}

- (IBAction)allClick:(id)sender {
    self.currentBtn.selected=NO;
    self.currentBtn=sender;
    self.currentBtn.selected=YES;
    self.type=@"all";
    if(self.flag==0){
        [self getXuanShang];
    }else{
        [self getQuestion];
    }
    
}
- (IBAction)myFaQiClick:(id)sender {
    self.currentBtn.selected=NO;
    self.currentBtn=sender;
    self.currentBtn.selected=YES;
    self.type=@"my";
    if(self.flag==0){
        [self getXuanShang];
    }else{
        [self getQuestion];
    }
}
- (IBAction)myJoinClick:(id)sender {
    self.currentBtn.selected=NO;
    self.currentBtn=sender;
    self.currentBtn.selected=YES;
    self.type=@"other";
    if(self.flag==0){
        [self getXuanShang];
    }else{
        [self getQuestion];
    }
}


#pragma mark tableview代理
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    KKCollectDocumentCell *question=(KKCollectDocumentCell *)cell;
    [question initQuestionData:self.datas[indexPath.section]];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.datas.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 70;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [tableView dequeueReusableCellWithIdentifier:@"KKCollectDocumentCell"];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 8;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section==0){
        return 8;
    }
    return 0.1;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    KKMyXuanShangM *question=self.datas[indexPath.section];
    KKXuanShangDetailVC *detail=[[KKXuanShangDetailVC alloc] init];
    detail.questionId=question.questionid;
    [self.navigationController pushViewController:detail animated:YES];
}

#pragma mark 接口--悬赏
-(void)getXuanShang{
    [self showDialog];
    [KKMyHttp getMyXuanShang:self.type success:^(id data) {
        self.datas=data;
        [self.tableView reloadData];
        [self dismissDialog];
    } failure:^(NSError *error) {
        [self dismissDialog];
    }];
}
#pragma mark 接口--提问
-(void)getQuestion{
    [self showDialog];
    [KKMyHttp getMyQuestion:self.type success:^(id data) {
        self.datas=data;
        [self.tableView reloadData];
        [self dismissDialog];
    } failure:^(NSError *error) {
        [self dismissDialog];
    }];
}
@end
